package com.lancoo.klgcourseware.base;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lancoo.klgcourseware.R;
import com.lancoo.klgcourseware.entity.bean.ErrorStates;
import com.lancoo.klgcourseware.entity.bean.KlgDbConstant;


/**
 * Created by pengmi on 2019/8/14 15:14
 * Description:
 */
public abstract class BaseKlgFragment extends Fragment implements View.OnClickListener{

    protected View convertView;
    protected RelativeLayout loadingView,netTipLayout;
    private ImageView img_errorLogo;
    private TextView tv_refreshPage;
    protected long time;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(convertView==null){
            convertView=inflater.inflate(getContentViewId(),container,false);
        }
        return convertView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    protected abstract int getContentViewId();

    protected abstract void initView(View view);

    protected void reloadData(){}


    /**
     * 加载动画的初始化
     */
    protected void showLoadingProgress(){
        ViewStub viewStub=  convertView.findViewById(R.id.viewstub_loading);
        if(viewStub!=null){
            viewStub.inflate();
            loadingView = convertView.findViewById(R.id.rl_loading);
        }else {
            loadingView.setVisibility(View.VISIBLE);
        }
        if (netTipLayout != null && netTipLayout.getVisibility() == View.VISIBLE) {
            netTipLayout.setVisibility(View.GONE);
        }
    }


    /**
     * 界面显示异常时的初始化
     */
    protected void showFailureLoad(ErrorStates errorStates){
        if(loadingView!=null){
            loadingView.setVisibility(View.GONE);
        }
//        if(xRefreshView!=null){
//            xRefreshView.stopLoadMore();
//            xRefreshView.stopRefresh();
//        }
        ViewStub stub=  convertView.findViewById(R.id.viewstub_error_network);
        if(stub!=null){
            stub.inflate();
            netTipLayout =  convertView.findViewById(R.id.rl_error);
            img_errorLogo = convertView.findViewById(R.id.img_error_logo);
            tv_refreshPage = convertView.findViewById(R.id.tv_refresh_page);
            tv_refreshPage.setOnClickListener(this);
        }else {
            netTipLayout.setVisibility(View.VISIBLE);
        }
        img_errorLogo.setImageResource(errorStates==ErrorStates.DATAERROR?R.mipmap.klg_icon_data_error:R.mipmap.klg_icon_network_error);
        tv_refreshPage.setVisibility(errorStates==ErrorStates.DATAERROR?View.GONE:View.VISIBLE);
    }

    protected void showEmptyLayout(){
        ViewStub viewStub = convertView.findViewById(R.id.viewstub_empty);
        if(viewStub!=null){
            viewStub.inflate();
        }
    }


    protected void dismissLoadingLayout(){
        if(loadingView!=null){
            loadingView.setVisibility(View.GONE);
        }
    }


    @Override
    public void onClick(View v) {
        int id=v.getId();
        if(id==R.id.tv_refresh_page){
            KlgDbConstant.klg_baseUrl=getContext().getSharedPreferences(KlgDbConstant.SP_KNOWLEDGE_COURSE, Context.MODE_PRIVATE).getString(KlgDbConstant.SP_KNOWLEDGE_COURSE_URL, KlgDbConstant.klg_baseUrl);
            netTipLayout.setVisibility(View.GONE);
            reloadData();
        }
    }

}
